<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
    
        function Person(name) {
            this.name = name;
        }
        var p1 = Person('刘德华');
        //p1.name;

        //闭包创建 私有的访问方式
        function createPerson(name) {
            var isBool = true;
            return {
                setName:function(name) {
                    this.name = name
                },
                getName:function() {
                    return this.name;
                },
                setSex:function(sex) {
                    if(isBool) {
                        isBool = false;
                        this.sex = sex;
                    } else {
                        console.log('性别只允许修改一次');                        
                    }
                },
                getSex:function() {
                    return this.sex;
                }
            }
        }

        var p2 = createPerson();
        p2.setName('张学友');
        console.log(p2.getName());

        p2.setSex('女');
        console.log(p2.setSex());

        p2.setSex('男');
        console.log(p2.setSex());
        
        
        
    </script>
</body>
</html>